package com.cskaoyan.service.wx.llk;

import com.cskaoyan.bean.wx.llk.*;
import com.cskaoyan.mapper.wx.llk.AddressMapper1;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    AddressMapper1 addressMapper;
    @Override
    public ItemsBean<AddressVO> queryList(Integer page, Integer limit,
                                          String name, Integer userId,
                                          String sort, String order) {

        PageHelper.startPage(page,limit);

        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        addressExample.setOrderByClause(sort + " " + order);

        List<Address> addressList = addressMapper.selectByExample(addressExample);

        PageInfo<Address> addressPageInfo = new PageInfo<>(addressList);
        int total = (int) addressPageInfo.getTotal();

        //啊啊啊 用了好笨的办法，想不到其他的了
        List<AddressVO> addressVOS = new ArrayList<>();
        for (Address address : addressList) {
            AddressVO addressVO = new AddressVO(addressMapper.selectRegionById(address.getAreaId()),
                    address.getIsDefault(),
                    address.getAreaId(),
                    address.getAddress(),
                    addressMapper.selectRegionById(address.getProvinceId()),
                    addressMapper.selectRegionById(address.getCityId()),
                    address.getName(),
                    address.getMobile(),
                    address.getId(),
                    address.getCityId(),
                    address.getUserId(),
                    address.getProvinceId());
            addressVOS.add(addressVO);
        }
        return ItemsBean.data(addressVOS,total);
    }

    @Override
    public List<AddressVo2> queryWxList(int id) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUserIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        if (addresses==null){return null;}
        AddressVo2[] addressVo2s = new AddressVo2[addresses.size()];
        for (int i = 0; i < addresses.size(); i++) {
            addressVo2s[i]=new AddressVo2();
            addressVo2s[i].setId(addresses.get(i).getId());
            addressVo2s[i].setName(addresses.get(i).getName());
            addressVo2s[i].setMobile(addresses.get(i).getMobile());
            addressVo2s[i].setIsDefault(addresses.get(i).getIsDefault());
            String[] name = addressMapper.selectAddress(addresses.get(i).getProvinceId(),addresses.get(i).getCityId(),addresses.get(i).getAreaId());
            addressVo2s[i].setDetailedAddress(name[0]+name[1]+name[2]+" "+addresses.get(i).getAddress());
        }

        return Arrays.asList(addressVo2s);
    }

    @Override
    public void delete(Map map) {
        Integer id = (Integer) map.get("id");
        Address address = new Address();
        address.setId(id);
        address.setDeleted(true);
        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public AddressVO3 queryObject(Integer id) {
        Address address = addressMapper.selectByPrimaryKey(id);
        String[] name = addressMapper.selectAddress(address.getProvinceId(),address.getCityId(),address.getAreaId());
        AddressVO3 addressVO3 = new AddressVO3(address.getIsDefault(), address.getAddress(), name[1], name[2], address.getName(),
                address.getMobile(), address.getId(), address.getAreaId(),
                address.getCityId(), address.getProvinceId(), name[0]);
        return addressVO3;
    }

    @Override
    public void updateAddress(Address address) {

        address.setUpdateTime(new Date());

        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public int insertAddress(Address address) {

        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
        address.setUserId(activeUser.getUserId());

        address.setUpdateTime(new Date());
        address.setAddTime(new Date());

        addressMapper.insertSelective(address);

        return address.getId();
    }


}
